import { useRequest } from 'umi';
import { useCallback, useMemo, useState } from 'react';
import { getCompanyID, getPosIDs, getUserDepID, getUserID, requestFunComponFiex } from '@/utils';
import { getBaseUrl } from '@/services/urlconst';

const baseUrl = getBaseUrl();

const SyncMaterialService = (curRes: Record<string, any>) => {
  /**
   * 保存当前的搜索参数
   * */
  const [manageTableParams, setManageTableParams] = useState<Record<string, any>>({});

  /**
   * 管理页获取数据方法
   * */
  const { tableProps, data, loading, run } = useRequest(
    (reqParams: Record<string, any>) => {
      let PostParams: Record<string, any> = {
        // current: 1,
        // pageSize: 10,
        page: 1,
        rows: 10,
        ...reqParams,
        // ...ManageTabSwitch(curRes),
      };

      let reqUrl = `${baseUrl}/purchaseNeedMaterialController/check`;

      /***保存一份参数**/
      setManageTableParams(PostParams);

      /***发起请求数据接口**/
      return requestFunComponFiex('POST', reqUrl, PostParams);
    },
    {
      manual: true,
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /**
   * 重新请求的方法
   * */
  const reloadRequest = useCallback((reqParams: Record<string, any> | any) => {
    run({ ...reqParams });
  }, []);

  /**
   * 页面数据及分页信息
   * */
  const tableValues = useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  return {
    dataRow: data,
    syncLoading: loading,
    reloadRequest,
    manageTableParams,
    ...tableValues, //页面列表值
  };
};

/**
 * 审核接口
 * */
export const AuditPlanMaterialServices = (callback: (res: any) => void) => {
  const request = useRequest(
    (payload) => {
      let bodyParams = {
        ...payload,
        checkerID: getUserID(),
        checkPositionID: getPosIDs(),
        checkDepartmentID: getUserDepID(),
        checkCompanyID: getCompanyID(),
        // dutyerID: getUserID(),
        // dutyPositionID: getPosIDs(),
        // dutyDepartmentID: getUserDepID(),
        // dutyCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/purchaseNeedMaterialController/check`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );

  return {
    auditPlanRun: request?.run,
    auditPlanLoading: request?.loading,
  };
};

export { SyncMaterialService };
